package com.befirefly.studentmanager.handler;

import com.befirefly.studentmanager.entity.StudentDO;
import com.befirefly.studentmanager.service.StudentService;
import com.befirefly.studentmanager.service.impl.StudentServiceImpl;
import com.befirefly.studentmanager.view.AddStudentView;
import com.befirefly.studentmanager.view.MainView;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author: 八千里路云和月
 * @Date: 2021/2/9 14:54
 * @Description:
 */
public class AddStudentViewHandler implements ActionListener {

    private AddStudentView addStudentView;
    private MainView mainView;

    public AddStudentViewHandler(AddStudentView addStudentView, MainView mainView) {
        this.addStudentView = addStudentView;
        this.mainView = mainView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if (StringUtils.equals("添加", text)) {
            StudentService studentService = new StudentServiceImpl();
            StudentDO studentDO = addStudentView.buildStudentDO();
            boolean addResult = studentService.add(studentDO);
            if (addResult) {
                // 重新加载表格查到最新数据
                mainView.reloadTable();
                addStudentView.dispose();
            } else {
                JOptionPane.showMessageDialog(addStudentView, "添加失败");
            }
        }
    }
}
